package com.nuoniu.sibanyun.controller.system;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.VerifyCode;
import com.nuoniu.sibanyun.service.VerifyCodeGenService;
import com.nuoniu.sibanyun.service.redis.RedisService;
import com.nuoniu.sibanyun.utils.IpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 图片验证码
 * @auther 小啊刚
 * @date 2020-08-01
 */
@Controller
@Api(value="图片验证码 ",tags={"图片验证码","APP_图片验证码"})
public class VerifyCodeController {

    @Autowired
    VerifyCodeGenService verifyCodeGenService;
    @Autowired
    private RedisService redisService;

    /**
     * 获取登录图片验证码
     * @author 小啊刚
     * @date 2020-08-01
     */
    @GetMapping("verifyCode/loginImageCode")
    @ApiOperation(value = "获取登录图片验证码" )
    public void verifyCode(String userPhone, HttpServletRequest request, HttpServletResponse response) {
        crateImgCode(request, response, "login_"  + userPhone + "_" );
    }

    /**
     * 获取注册图片验证码
     * @author 小啊刚
     * @date 2020-08-01
     */
    @GetMapping("verifyCode/registerImageCode")
    @ApiOperation(value = "获取注册图片验证码" )
    public void registerImageCode(String userPhone, HttpServletRequest request, HttpServletResponse response) {
        if(StringUtils.isBlank(userPhone) || userPhone.length() < 11) {
            return;
        }
        crateImgCode(request, response, "register_" + userPhone + "_");
    }

    /**
     * 获取忘记密码图片验证码
     * @author 小啊刚
     * @date 2020-08-01
     */
    @GetMapping("verifyCode/forgetPwdImageCode")
    @ApiOperation(value = "获取忘记密码图片验证码" )
    public void forgetPwdImageCode(HttpServletRequest request, HttpServletResponse response) {
        crateImgCode(request, response, "forget_pwd_");
    }

    @GetMapping("verifyCode/invitationImageCode")
    @ApiOperation(value = "邀约表单")
    public void invitationImageCode(String userPhone, HttpServletResponse response, HttpServletRequest request) {
        crateImgCode(request, response, "invitation_" + userPhone + "_");
    }

//    /**
//     * 获取公司入职图片验证码
//     * @author 小啊刚
//     * @date 2020-08-01
//     */
//    @GetMapping("verifyCode/entryImageCode")
//    public void entryImageCode(HttpServletRequest request, HttpServletResponse response) {
//        crateImgCode(request, response, "entry_");
//    }

    /**
     * 生成图片验证码
     * @author 小啊刚
     * @date 2020-08-12
     */
    public void crateImgCode(HttpServletRequest request, HttpServletResponse response, String type) {
        try {
            //设置长宽
            VerifyCode verifyCode = verifyCodeGenService.generate(80, 28);
            String code = verifyCode.getCode();
            //将VerifyCode绑定redis
            String ip = IpUtil.getClientIp(request);
            redisService.set(type + ip, code, 180);
            //设置响应头
            response.setHeader("Pragma", "no-cache");
            //设置响应头
            response.setHeader("Cache-Control", "no-cache");
            //在代理服务器端防止缓冲
            response.setDateHeader("Expires", 0);
            //设置响应内容类型
            response.setContentType("image/jpeg");
            response.getOutputStream().write(verifyCode.getImgBytes());
            response.getOutputStream().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 检查登录验证码
     * @author 小啊刚
     * @date 2020-08-01
     */
    @GetMapping(value = "verifyCode/checkLoginValidCode/{validCode}/{userName}")
    @ApiOperation(value = "检查登录验证码" )
    @ResponseBody
    public HttpResult checkCode(@PathVariable String validCode, @PathVariable String userName, HttpServletRequest request) {
        String ip = IpUtil.getClientIp(request);
        if(!redisService.hasKey("login_" + userName + "_"+ ip)) {
            throw new NuoNiuException("验证码已过期！");
        }

        String smsCode = redisService.get("login_" + userName + "_" + ip).toString();
        if(smsCode.toLowerCase().equals(validCode.toLowerCase())) {
            return HttpResult.ok();
        }
        throw new NuoNiuException("验证码不正确！");
    }


}
